new Vue({
    el: '#app',
    data() {
        return {
            rules: {
                Start_Date: [{ required: true, message: '请输入开始时间', trigger: 'blur' }],
                End_Date: [{ validator: this.validateEnd_Date, trigger: 'blur' }],
            },
            form: {
                Member_Share_Config_SerialNumber: "",
                Type_Date: '0',
                Start_Date: '',
                End_Date: '',
                Enterprise_Info_SerialNumber: $.cookie("enterprise"),
                Children: {
                    member_share_config_article: [{
                        Member_Share_Config_Article_SerialNumber: "",
                        Member_Share_Config_SerialNumber: "",
                        Article_SerialNumber: '',//适用商品
                        Divide_Type: '',//方案类型
                        Divide_Amount: '',//适用金额
                        Divided_Days: '',//天数
                    }]
                }
            },
            arr: [{
                value: '0',
                label: '指定分销金额'
            }, {
                value: '1',
                label: '根据订单成交额的百分比'
            }],
            article: [],
            loading: false,
            dialogVisible: false,//上传
            fileList: [],//文件存放
            tableData: [],//表格预览
        }
    },
    mounted() {
        this.getData()
    },
    methods: {
        validateEnd_Date(rule, value, callback) {//校验结束时间
            if (!value && this.form.Type_Date == '0') {
                callback(new Error('短期方案结束时间不能为空'));
            } else if (+new Date(value) < +new Date(this.form.Start_Date)) {
                callback(new Error('结束时间不能小于开始时间'));
            } else {
                callback();
            }
        },
        getData() {
            let _this = this;
            $.ajax({
                url: DomainName + '/getInquiryAction?token=' + $.cookie("token"),
                type: 'POST',
                data: {
                    Data: 'article_info'
                },
                success: function (res) {
                    _this.article = res;
                },
                complete: function (XMLHttpRequest) {
                    _this.loading = false
                    if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
                        window.parent.document.location.href = absolutepath() + XMLHttpRequest.getResponseHeader("CONTENTPATH");
                    }
                }
            })
        },

        add_member_share_config_article() {
            this.form.Children.member_share_config_article.push({
                Member_Share_Config_Article_SerialNumber: "",
                Member_Share_Config_SerialNumber: "",
                Article_SerialNumber: '',//适用商品
                Divide_Type: '',//方案类型
                Divide_Amount: '',//适用金额
                Divided_Days: '',//天数
            })
        },
        remove_member_share_config_article() {
            if (this.form.Children.member_share_config_article.length > 1) {
                this.form.Children.member_share_config_article.pop()
            } else {
                this.$message({
                    message: '不能在删除了',
                    type: 'warning'
                })
            }
        },
        handleDate(e) {//长期短期
            console.log(e);
            if (e == '1') {
                this.form.End_Date = ''
            }
        },
        submitForm(formName) {
            let _this = this
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    _this.loading = true
                    $.ajax({
                        url: DomainName + '/ShareConfigAdd?token=' + $.cookie('token'),
                        type: 'POST',
                        data: {
                            Data: JSON.stringify({
                                member_share_config: [_this.form]
                            }),
                        },
                        success: function (res) {
                            console.log('提交结果', res)
                            _this.$message.success("提交成功")
                            window.history.go(-1);
                        },
                        error: function () {
                            _this.$message.error("操作失败，请稍后再试")
                        },
                        complete: function (XMLHttpRequest) {
                            _this.loading = false
                            if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
                                window.parent.document.location.href = absolutepath() + XMLHttpRequest.getResponseHeader("CONTENTPATH");
                            }
                        }
                    })
                } else {
                    console.log("error submit!!");
                    return false;
                }
            });
        },
        download() {
            window.location.href = './上传模板.xlsx'
        },
        submit() {
            const _this = this
            _this.loading = true
            let obj = {
                Member_Share_Config_SerialNumber: "",
                Type_Date: this.form.Type_Date,
                Start_Date: this.form.Start_Date,
                End_Date: this.form.End_Date,
                Enterprise_Info_SerialNumber: $.cookie("enterprise"),
                Children:{
                    member_share_config_article:this.tableData
                }
            }
            $.ajax({
                url: DomainName + '/ShareConfigAdd?token=' + $.cookie('token'),
                type: 'POST',
                data: {
                    Data: JSON.stringify({
                        member_share_config: [obj]
                    }),
                },
                success: function (res) {
                    console.log('提交结果', res)
                    _this.$message.success("提交成功")
                    window.history.go(-1)
                },
                error: function () {
                    _this.$message.error("操作失败，请稍后再试")
                },
                complete: function (XMLHttpRequest) {
                    _this.loading = false
                    if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
                        window.parent.document.location.href = absolutepath() + XMLHttpRequest.getResponseHeader("CONTENTPATH");
                    }
                }
            })
        },
        uploadDetails(file) {
            this.fileList.push({
                name: file.name,
                url: file.type
            })
            const _this = this
            var reader = new FileReader();
            reader.readAsBinaryString(file);
            reader.onload = function (e) {
                var data = e.target.result;
                var wb = XLSX.read(data, {
                    type: 'binary' //以二进制的方式读取
                });
                var sheet0 = wb.Sheets[wb.SheetNames[0]];//sheet0代表excel表格中的第一页
                var json = XLSX.utils.sheet_to_json(sheet0);//利用接口实现转换。
                json.forEach(item => {
                    _this.tableData.push({
                        Member_Share_Config_Article_SerialNumber: "",
                        Member_Share_Config_SerialNumber: "",
                        Article_SerialNumber:item['商品id'],
                        Article_Name:item['商品名称'],
                        Divide_Type:item['方案类型']=='指定分销金额'?'0':'1',
                        Divide_Amount:item['分销金额'],
                        Divided_Days:item['分润天数']
                    })      
                })
            }
        },
        beforeRemove() {
            this.fileList.pop()
            this.tableData = []
        },
        resetForm(formName) {
            this.$refs[formName].resetFields();
        },
    }
})